<?php
/**
 * 记账接口
 * @author solar
 */
class BillProxy extends BaseProxy
{
	/**
	 * 记账
	 * @author solar
	 */
	public function billing() {
		//$_POST['users']=explode(',',$_POST['user_ids']);
		if($_POST['users']===null || !is_array($_POST['users']) || count($_POST['users'])===0)
			return $this->fail('请选择参与的用户 ');
		$v = new Validator();
		$v->setRule('checkout_id',array('账单ID','required','int'=>array('isPositive'=>true)));
		$vResult = $v->validate($_POST);		
		if($vResult!==true) return $this->fail($vResult);
		$checkoutRow=ModelFactory::get('CheckoutModel')->get($_POST['checkout_id']);
		if(!$checkoutRow) return $this->fail('找不到当前账单['.$_POST['checkout_id'].']');
		if($checkoutRow['c_status']!=0) return $this->fail('当前账单不是未结账状态，不能记账');
		$memberIDs=ModelFactory::get('GroupUserModel')->getUserIDs($checkoutRow['g_id']);
		$billItemList=array();
		$paySum=0; $consumeSum=0;
		foreach($_POST['users'] as $user_id){
			$aRule = array('用户ID','required','int'=>array('isPositive'=>true));
			$vResult=$v->validateOne('user_id',$user_id,$aRule,true);
			if($vResult!==true) return $this->fail($vResult);
			if(!in_array($user_id,$memberIDs)) 
				return $this->fail('user_id['.$user_id.']不在当前账单组中');
			$payKey='pay'.$user_id;
			$aRule = array('支出金额','number'=>array('min'=>0));
			$vResult=$v->validateOne($payKey,$_POST[$payKey],$aRule,true);
			if($vResult!==true) return $this->fail($vResult);
			$pay=round(floatval($_POST[$payKey]),2);
			$paySum+=$pay;
			$billItemList[$user_id]['bi_pay']=$pay;
			$consumeKey='consume'.$user_id;
			$aRule = array('消费金额','number'=>array('min'=>0));
			$vResult=$v->validateOne($consumeKey,$_POST[$consumeKey],$aRule,true);
			if($vResult!==true) return $this->fail($vResult);
			$consume=round(floatval($_POST[$consumeKey]),2);
			$consumeSum+=$consume;
			$billItemList[$user_id]['bi_consume']=$consume;
			$billItemList[$user_id]['u_id']=$user_id;
			if($pay==0 && $consume==0) unset($billItemList[$user_id]);
		}
		if($paySum==0) return $this->fail('请输入支出金额');
		if($consumeSum==0) return $this->fail('请输入消费金额');
		$balance=$paySum-$consumeSum;
		if(abs($balance)>=1) return $this->fail('支出总金额与消费总金额不相等');
		$aRule = array('备注','required','length'=>array('max'=>150));
		$vResult=$v->validateOne('bill_note',$_POST['bill_note'],$aRule,true);
		if($vResult!==true) return $this->fail($vResult);
		$_POST['bill_note'] = htmlentities($_POST['bill_note'], ENT_NOQUOTES, 'UTF-8');
		$billService=ServiceFactory::get('BillService');
		if($balance!==0) $billService->fixBillItems($balance,$billItemList);
		$b_id = $billService->createBill($checkoutRow['g_id'],$_POST['checkout_id'],$_POST['bill_note'],$paySum,$billItemList);
		if($b_id===false) return $this->error(Gateway::InternalError);
		return $this->success(ModelFactory::get('BillModel')->get($b_id));
	}
	
	/**
	 * 当前账单
	 * @author solar
	 */
	public function current() {
		$validator=new Validator();
		$validator->setRule('group_id',array('账单组ID','required','int'=>array('isPositive'=>true)));
		$validation=$validator->validate($_GET);
		if($validation!==true) return $this->fail($validation);
		$checkoutRow=ModelFactory::get('CheckoutModel')->getUncheckout($_GET['group_id']);
		if(!$checkoutRow) return $this->fail('找不到未结账单');
		if(!ServiceFactory::get('GroupService')->hasPrivilege($checkoutRow['g_id'])){
			return $this->fail('您不是该账单组的成员，没有权限查看');
		}
		$billModel=ModelFactory::get('BillModel');
		$aWhere['c_id']=$checkoutRow['c_id'];
		$data['count']=$billModel->getCount($aWhere);
		$data['list']=$billModel->getList($this->getOffset(),$this->getLimit(),'b_id','DESC',$aWhere);
		$checkUserModel=ModelFactory::get('CheckoutUserModel');
		$data['checkout_id']=$checkoutRow['c_id'];
		$data['user_id'] = CoreFactory::session()->get('user_id');
		return $this->success($data);
	}
	
	/**
	 * 记账记录
	 * @author solar
	 */
	public function listing() {
		$validator=new Validator();
		$validator->setRule('checkout_id',array('账单ID','required','int'=>array('isPositive'=>true)));
		$validation=$validator->validate($_GET);
		if($validation!==true) return $this->fail($validation);
		$checkoutRow=ModelFactory::get('CheckoutModel')->get($_GET['checkout_id']);
		if(!$checkoutRow) return $this->fail('找不到账单['.$_GET['checkout_id'].']');
		if(!ServiceFactory::get('GroupService')->hasPrivilege($checkoutRow['g_id'])){
			return $this->fail('您不是该账单组的成员，没有权限查看');
		}
		$billModel=ModelFactory::get('BillModel');
		$aWhere['c_id']=$_GET['checkout_id'];
		$data['count']=$billModel->getCount($aWhere);
		$data['list']=$billModel->getList($this->getOffset(),$this->getLimit(),'b_id','DESC',$aWhere);
		return $this->success($data);
	}
	
	/**
	 * 记账详情
	 * @author solar
	 */
	public function detail() {
		$v = new Validator();
		$v->setRule('bill_id',array('记账ID','required','int'=>array('isPositive'=>true)));
		$vResult=$v->validate($_GET);
		if($vResult!==true) return $this->fail($vResult);
		$data['bill']=ModelFactory::get('BillModel')->get($_GET['bill_id']);
		if(!$data['bill']) return $this->fail('找不到记账记录');
		$data['billItems']=ModelFactory::get('BillItemModel')->getAll(array('b_id'=>$_GET['bill_id']));
		return $this->success($data);
	}
	
	/**
	 * 个人汇总
	 * @author solar
	 */
	public function mysummary() {
		$v = new Validator();
		$v->setRule('checkout_id',array('账单ID','required','int'=>array('isPositive'=>true)));
		$vResult = $v->validate($_GET);		
		if($vResult!==true) return $this->fail($vResult);
		$checkUserModel = ModelFactory::get('CheckoutUserModel');
		$aSummary = $checkUserModel->getCheckoutUser(CoreFactory::session()->get('user_id'),$_GET['checkout_id']);
		return $this->success($aSummary);
	}
	
	/**
	 * 删除记账
	 * @author solar
	 */
	public function remove() {
		$v = new Validator();
		$v->setRule('bill_id',array('记账ID','required','int'=>array('isPositive'=>true)));
		$vResult = $v->validate($_POST);
		if($vResult!==true) return $this->fail($vResult);
		$billRow = ModelFactory::get('BillModel')->get($_POST['bill_id']);
		if(empty($billRow)) return $this->fail('找不到记账记录');
		$checkoutRow = ModelFactory::get('CheckoutModel')->get($billRow['c_id']);
		if(empty($checkoutRow)) return $this->fail('找不到账单');
		if($checkoutRow['c_status']!=0) return $this->fail('账单是已结束状态，不能删除');
		$billService = ServiceFactory::get('BillService');
		$result = $billService->removeBill($_POST['bill_id']);
		if($result){
			$billService->computeSummary($billRow['c_id']);
			return $this->success();
		} else return $this->error(Gateway::InternalError);
	}
	
}
